{% extends "app_02/layout.html" %}

{% block content %}
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">表单</div>
        <div class="panel-body">
            <form id="formAdd">
                <div class="clearfix">
                    {% for field in form %}
                    <div class="col-xs-6">
                        <div class="form-group" style="position: relative;margin-bottom: 20px">
                            <label>{{ field.label }}</label>
                            {{ field }}
                            <span class="error-msg" style="color: red;position: absolute;"></span>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                <div class="col-xs-12">
                    <button id="btnAdd" type="button" class="btn btn-primary">提交</button>
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-default">
        <!-- Default panel contents -->
        <div class="panel-heading">
            <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
            任务列表
        </div>

        <!-- Table -->
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>ID</th>
                <th>标题</th>
                <th>级别</th>
                <th>负责人</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% for obj in queryset %}
            <tr>
                <th>{{ obj.id }}</th>
                <th>{{ obj.title }}</th>
                <th>{{ obj.get_level_display }}</th>
                <th>{{ obj.user.username }}</th>
                <td>
                    <a class="btn btn-primary btn-xs" href="/app_02/admin/{{ obj.id }}/edit/">编辑</a>
                    <a class="btn btn-danger btn-xs" href="/app_02/admin/{{ obj.id }}/delete/">删除</a>
                </td>
            </tr>
            {% endfor %}


            <!--    <h1>Ajax学习</h1>-->

            <!--    <h3>示例1</h3>-->
            <!--    <input type="button" id="btn1" class="btn btn-primary" value="点击1"/>-->

            <!--    <h3>示例二</h3>-->
            <!--    <input type="text" id="txtuser" placeholder="用户名">-->
            <!--    <input type="text" id="txtage" placeholder="年龄">-->
            <!--    <input type="button" id="btn2" class="btn btn-primary" value="点击2"/>-->

            <!--    <h3>示例三</h3>-->
            <!--    <form id="form3">-->
            <!--        <input type="text" name="user" placeholder="用户名">-->
            <!--        <input type="text" name="age" placeholder="年龄">-->
            <!--        <input type="text" name="email" placeholder="邮箱">-->
            <!--        <input type="text" name="more" placeholder="介绍">-->
            <!--        <input type="button" id="btn3" class="btn btn-primary" value="点击3"/>-->
            <!--    </form>-->

            </tbody>
        </table>

    </div>


    <ul class="pagination">
        {{ page_string }}
    </ul>
</div>
{% endblock %}


{% block js %}
<script type="text/javascript">
    $(function () {
        // 页面框架加载完成之后代码自动执行
        bindBtn1Event();
        bindBtn2Event();
        bindBtn3Event();
        bindBtnAddEvent();
    })

    function bindBtn1Event() {
        $("#btn1").click(function () {
            $.ajax({
                url: '/app_02/task/ajax/',
                type: 'post',
                data: {
                    n1: 1231,
                    n2: 1341,
                },
                dataType: "JSON",
                success: function (res) {
                    console.log(res);
                    console.log(res.status);
                    console.log(res.data);
                }
            })
        })
    }

    function bindBtn2Event() {
        $("#btn2").click(function () {
            $.ajax({
                url: "/app_02/task/ajax/",
                type: "post",
                data: {
                    name: $("#txtuser").val(),
                    age: $("#txtage").val()
                },
                dataType: "JSON",
                success: function (res) {
                }
            })
        })
    }

    function bindBtn3Event() {
        $("#btn3").click(function () {
            $.ajax({
                url: '/app_02/task/ajax/',
                type: "post",
                data: $("#form3").serialize(),
                dataType: "JSON",
                success: function (res) {
                }
            })
        })
    }

    function bindBtnAddEvent() {
        $("#btnAdd").click(function () {
            $(".error-msg").empty();

            $.ajax({
                url: '/app_02/task/add/',
                type: 'post',
                data: $("#formAdd").serialize(),
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        alert("添加成功")
                        //实现页面的刷新
                        location.reload()
                    } else {
                        $.each(res.error, function (name, data) {
                            // console.log(name, data); name 名， data错误信息[""]

                            $("#id_" + name).next().text(data[0])
                        })
                    }
                }
            })
        })
    }

</script>
{% endblock %}